﻿<%@ WebHandler Language="C#" Class="movies" %>

using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间

public class movies : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        context.Response.Write(formDataUpload(context));
    }


    private static string formDataUpload(HttpContext context)
    {

        HttpFileCollection postedFile = context.Request.Files;
        bool fileUploaded = false;
        string name = context.Request.Form["name"];
        string author = context.Request.Form["author"];
        string uptime = context.Request.Form["uptime"];
        string type = context.Request.Form["type"];
        string intro = context.Request.Form["intro"];
        string imgPath = "";
        string videoPath = "";

        string msg = string.Empty;
        string error = string.Empty;

        // "++"

        if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(author) || String.IsNullOrEmpty(uptime)|| String.IsNullOrEmpty(type) || String.IsNullOrEmpty(intro))
        {
            return "{\"error\":\"2\", \"msg\":\"表单项不能为空，请重新填写！\"}";
        }

        if (postedFile.Count > 0)
        {
            string savepath = "";
            string tempPath = "";
            tempPath = System.Web.HttpContext.Current.Server.MapPath("..\\upload\\");//System.Configuration.ConfigurationManager.AppSettings["FolderPath"];  
            savepath = tempPath; //context.Server.MapPath(tempPath);  

            for (int i = 0; i < postedFile.Count; i++)
            {
                string filename = postedFile[i].FileName;
               
                
                if (!String.IsNullOrEmpty(filename))
                {
                    string sExtension = filename.Substring(filename.LastIndexOf('.'));
                    if (!Directory.Exists(savepath))
                        Directory.CreateDirectory(savepath);

                    // string sNewFileName = filename.Substring(0, filename.IndexOf("."));


                    string sNewFileName = Guid.NewGuid().ToString("N");
                    if (i == 0)
                    {
                        imgPath = @"upload/" + sNewFileName + sExtension;
                    }
                    else
                    {
                        videoPath = @"upload/" + sNewFileName + sExtension;
                    }
                    postedFile[i].SaveAs(savepath + @"\" + sNewFileName + sExtension);
                    
                    msg = " 成功! ";
                    error = "0";
                    fileUploaded = true;
                }
                else
                {
                    msg = " 文件未选择! ";
                    error = "1";
                }
                
            }


        }
        else
        {
            msg = "没有选择文件";
            error = "1";
            return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
        }

        if (fileUploaded)
        {
            DBHelper.sqlMethodNoDataset("insert into movielist(mname,maker,url,img,isrecommend,onlinetime,breif,tid) values('" + name + "','" + author + "','" + videoPath + "','" + imgPath + "',0,'" + uptime + "','" + intro + "'," + type + ");");
        }


        return "{\"error\":\"" + error + "\", \"msg\":\"" + msg + "\"}";
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}